<template>
  <div class="category-management">
    <div class="header">
      <el-button type="info" icon="el-icon-plus" @click="openDialog('1')"
        >新增菜品分类</el-button
      >
      <el-button type="warning" icon="el-icon-plus" @click="openDialog('2')"
        >新增套餐分类</el-button
      >
    </div>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column prop="name" label="分类名称"> </el-table-column>
      <el-table-column prop="type" label="分类管理">
        <template v-slot="{ row }">
          {{ row.type == "1" ? "菜品分类" : "套餐分类" }}
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="操作时间"> </el-table-column>

      <el-table-column prop="sort" label="排序"></el-table-column>
      <el-table-column label="操作">
        <template v-slot="{ row }">
          <el-link type="primary" @click="openDialog(formData.type, row)"
            >修改</el-link
          >
          <el-divider direction="vertical"></el-divider>
          <el-popconfirm
            title="此操作将永久删除该文件, 是否继续?"
            @onConfirm="handleChangeStatus(row)"
          >
            <el-link type="danger" slot="reference">删除</el-link>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div class="footer">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageInfo.page"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="pageInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>

    <el-dialog
      :title="`${isEdit ? '编辑' : '新增'}${
        formData.type == '1' ? '菜品' : '套餐'
      }分类`"
      :visible.sync="dialogVisible"
    >
      <el-form :model="formData" label-width="80px">
        <el-form-item label="分类名称">
          <el-input
            v-model="formData.name"
            placeholder="请输入分类名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="排序">
          <el-input
            v-model="formData.sort"
            type="number"
            placeholder="请输入排序值"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="closeDialog">取消</el-button>
        <el-button type="primary" @click="addCategory">确认</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getCategory,
  changeStatus,
  addCategory,
  changeCategory,
} from "@/api/classification";
export default {
  data() {
    return {
      dialogtitle: "",
      dialogVisible: false,
      formData: {
        name: "",
        type: "1",
        sort: "",
      },
      isEdit: false,
      input: "",
      tableData: [],
      pageInfo: {
        page: 1,
        pageSize: 10,
      },
      total: 0,
    };
  },
  created() {
    this.init();
  },
  watch: {
    dialogVisible(newVal) {
      if (!newVal) {
        this.formData = {};
      }
    },
  },
  methods: {
    openDialog(type, row) {
      if (row) {
        this.formData = JSON.parse(JSON.stringify(row));
        this.isEdit = true;
      } else {
        this.isEdit = false;
        this.formData.type = type;
      }
      this.dialogVisible = true;
    },
    closeDialog() {
      this.dialogVisible = false;
    },
    async addCategory() {
      if (!this.formData.name) {
        this.$message.error("请填写完整信息");
        return;
      }
      if (this.isEdit) {
        await changeCategory(this.formData);
        this.$message.success("修改成功");
      } else {
        await addCategory(this.formData);
        this.$message.success("新增成功");
      }

      this.dialogVisible = false;
      this.init();
    },

    init() {
      getCategory({
        ...this.pageInfo,
        name: this.input,
      }).then((res) => {
        this.tableData = res.data.records;
        this.total = res.data.total;
      });
    },
    handleSizeChange(val) {
      this.pageInfo.page = 1;
      this.pageInfo.pageSize = val;
      this.init();
    },
    handleCurrentChange(val) {
      this.pageInfo.page = val;
      this.init();
    },
    async handleChangeStatus(row) {
      await changeStatus({
        id: row.id,
        status: +!row.status,
      });
      this.$message.success("改变成功");
      this.init();
    },
  },
};
</script>

<style lang="scss" scoped>
.category-management {
  padding: 33px;
  margin: 10px;
  background-color: #fff;
  .header {
    display: flex;

    .el-input {
      width: 250px;
      .el-icon-search {
        cursor: pointer;
      }
    }
  }
  .el-table {
    margin-top: 20px;
  }
  .footer {
    margin-top: 30px;
    display: flex;
    justify-content: center;
  }
}
</style>
